﻿CREATE PROCEDURE [util].[GetScheduleExceptions]
@CheckDate SMALLDATETIME, @ExceptionType NVARCHAR (128)=NULL, @ExceptionCount INT OUTPUT
AS
BEGIN
	DECLARE @RC int;
	SET @RC = 0;
	
	IF (@ExceptionType IS NOT NULL)
	AND NOT EXISTS(
		SELECT * FROM [util].[ScheduleExceptionTypes]
		WHERE ([Name] = @ExceptionType)
		)
	BEGIN
		RAISERROR('The schedule exception [%s] is not configured on this instance of DBADb. Contact the database administration team for assistance.',16,1,@ExceptionType);
		SET @RC = @@ERROR;
	END;
	
	SELECT @ExceptionCount = COUNT(*)
	FROM [util].[ScheduleExceptions] AS ex
	INNER JOIN [util].[ScheduleExceptionTypes] AS tp
		ON (tp.[Id] = ex.[TypeId])
	WHERE  (ex.[DateKey] = @CheckDate)
		AND(tp.[Name] = ISNULL(@ExceptionType,[Name]));

	SET @RC = @@ERROR;
	
	RETURN(@RC);
END